<?php
namespace App\Libraries\Decorator\Order;


use App\Libraries\Decorator\DecoratorInterface\ModelDecorator;
use App\Libraries\Decorator\Image\CoverDecorator;
use App\Libraries\Tools\ConstVar;
use App\Libraries\Tools\Tools;
use App\Libraries\Tools\URL;
use App\Models\Order\Order;
use App\Models\Post\Post;
use App\Models\Product\Product;
use App\Models\Shop\ShopCoupon;
use Illuminate\Support\Facades\Cache;

class OrderDecorator implements ModelDecorator
{

    public function before(){
        //todo
    }

    public function after(&$data){
        if(!is_array($data)){
            $data = json_decode(json_encode($data), true);
        }
        if (isset($data['list'])) {
            // 一维数组
            foreach($data['list'] as &$v){
                switch($v['type']){
                    case ConstVar::$ORDER_TYPE_POST:
                        $post = Post::where('id', $v['object_id'])->first();
                        if(empty($post)){
                            $v['object_title'] = '主题已被删除';
                            $v['object_content'] = '主题已被删除';
                            $v['object_cover'] = '';
                        }else{
                            $v['object_title'] = '主题';
                            $v['object_content'] = Tools::cutString($post->post, 15);
                            $v['object_cover'] = '';
                        }
                        break;
                    case ConstVar::$ORDER_TYPE_PRODUCT:
                        $product = Product::where('id', $v['object_id'])->first();
                        if(empty($product)){
                            $v['object_title'] = '商品已被下架';
                            $v['object_content'] = '商品已被下架';
                            $v['object_cover'] = '';
                        }else{
                            $v['object_title'] = $product->title;
                            $v['object_content'] = $product->title;
                            $v['object_cover'] = URL::getImage($product->cover);
                        }
                        break;
                    case ConstVar::$ORDER_TYPE_TRANSFER_TO_SHOP:
                        $v['object_title'] = 'TRANSFER';
                        $v['object_content'] = 'TRANSFER';
                        $v['object_cover'] = '';
                        break;
                    case ConstVar::$ORDER_TYPE_COUPON:
                        $shopCoupon = new ShopCoupon();
                        $shopCoupon->addDecorator(new CoverDecorator());
                        $v['object'] = $shopCoupon->detail($v['object_id'], false, true);
//                        if(empty($coupon)){
//                            $v['object_title'] = '套餐已被下架';
//                            $v['object_content'] = '套餐已被下架';
//                            $v['object_cover'] = '';
//                        }else{
//                            $v['object_title'] = $coupon->title;
//                            $v['object_content'] = $coupon->content;
//                            $v['object_cover'] = URL::getImage($coupon->cover);
//                        }
                        break;
                    default:
                        break;
                }
                $v['payment_info'] = Cache::get($v['out_trade_no']);
                if(empty($v['payment_info']) && $v['status'] == ConstVar::$ORDER_STATUS_WAITING){
                    $order = Order::where('id', $v['id'])->first();
                    $order->status = ConstVar::$ORDER_STATUS_EXPIRED;
                    $order->save();
                    $v['status'] = ConstVar::$ORDER_STATUS_EXPIRED;
                }
            }
        }
        if(is_array($data)){
            foreach($data as &$v){
                $this->after($v);
            }
        }
        return $data;
    }
}